/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.model;

import com.tamnhat.entity.MediaItem;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class GameAndroidModel {

    public static ArrayList<MediaItem> getAll() throws Exception{
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        ArrayList<MediaItem> listGame = new ArrayList<MediaItem>();

        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery("select * "
                    + "from md_item_properties,md_items "
                    + "where md_items.media_type = 'game-android' and md_item_properties.item_id = md_items.item_id "
                    + "order by md_item_properties.item_id");
            MediaItem m = null;
            Properties p = null;
            String itemId = "";
            while (rs.next()) {
                String check = rs.getString(2);
                if (!check.equalsIgnoreCase(itemId)) {
                    itemId = check;
                    if (p != null) {
                        m.setProperties(p);
                        listGame.add(m);
                    }
                    m = new MediaItem();
                    p = new Properties();
                    m.setItemId(itemId);
                    m.setDownloadCount(Integer.parseInt(rs.getString(11)));
                    m.setViewCount(Integer.parseInt(rs.getString(10)));
                }
                if (rs.getString(4) == null) {
                    p.put(rs.getString(3), "");
                } else {
                    p.put(rs.getString(3), rs.getString(4));
                }
            }
            return listGame;
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
    }

    public static ArrayList<MediaItem> getListCondition(int size, String itemBegin) throws Exception{
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        ArrayList<MediaItem> listGame = new ArrayList<MediaItem>();
        String condition = "";
        if (!itemBegin.equals("")) {
            condition = "and md_items.item_id > " + itemBegin + " ";
        }
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery("select * "
                    + " from md_item_properties,md_items "
                    + " where md_items.media_type = 'game-android' and md_item_properties.item_id = md_items.item_id " + condition
                    + " order by md_item_properties.item_id");
            MediaItem m = null;
            Properties p = null;
            String itemId = "";
            while (rs.next()) {
                String check = rs.getString(2);
                if (!check.equalsIgnoreCase(itemId)) {
                    itemId = check;
                    if (p != null) {
                        m.setProperties(p);
                        listGame.add(m);
                        if (listGame.size() == size) {
                            itemBegin = m.getItemId();
                            return listGame;
                        }
                    }
                    m = new MediaItem();
                    p = new Properties();
                    m.setItemId(itemId);
                    m.setDownloadCount(Integer.parseInt(rs.getString(11)));
                    m.setViewCount(Integer.parseInt(rs.getString(10)));
                }
                if (rs.getString(4) == null) {
                    p.put(rs.getString(3), "");
                } else {
                    p.put(rs.getString(3), rs.getString(4));
                }
            }
            return listGame;
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
    }

}
